﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BuildOptimizer
{
    class BoCommandBuildNydusWorm : BoCommand
    {
        private const float _min = 100.0f;
        private const float _gas = 100.0f;
        private const int _time = 20;
        private const commandID _commandID = commandID.buildNydusWorm;

        public BoCommandBuildNydusWorm()
        {
        }

        public override void execute(BoGoal goal, BoDecisionNode node)
        {
            node.gameState.removeMineralsAndGas(_min, _gas);
            node.gameState.startBuildingBuilding((int)Buildings.NydusWorm);

            checkIfMoreRequired(goal, node);
            BoTask t = new BoTask(_time, delegate(BoDecisionNode n)
            {
                n.gameState.finishedBuildingBuilding((int)Buildings.NydusWorm);
            });
            node.gameState.addTask(t);
        }

        public static bool checkIfMoreRequired(BoGoal goal, BoDecisionNode node)
        {
            int nbRequired = goal.nbBuildingsRequired((int)Buildings.NydusWorm);
            int nbCurrently = node.gameState.nbBuildingsBeingBuilt((int)Buildings.NydusWorm) + node.gameState.nbBuildingsBuilt((int)Buildings.NydusWorm);

            if (nbRequired == -1)
            {
                if (nbCurrently < goal.nbBuildingsRequired((int)Buildings.NydusNetwork))
                {
                    return true;
                }
            }
            if (nbCurrently < nbRequired)
            {
                return true;
            }

            node.dismissCommand((int)commandID.buildNydusWorm);
            return false;
        }
        public static bool isAvailable(BoGameState gs)
        {
            int nbNydusNetworks = gs.nbBuildingsBuilt((int)Buildings.NydusNetwork);
            int nbNydusWorms = gs.nbBuildingsBuilt((int)Buildings.NydusWorm) + gs.nbBuildingsBeingBuilt((int)Buildings.NydusWorm);

            if ((nbNydusNetworks - nbNydusWorms) < 1)
            {
                return false;
            }
            return true;
        }
        public static bool isAffordable(BoGameState gs)
        {
            if (!gs.isResourcesAvailable(_min, _gas, 0))
            {
                return false;
            }
            return true;
        }

        public override int getCommandCode()
        {
            int code = ((int)_commandID * 100);
            return code;
        }
        public override commandID getCommandID()
        {
            return _commandID;
        }
        public override string toString()
        {
            string str = "Nydus Worm";
            return str;
        }
    }
}
